<?php

require FCPATH . '/application/controllers/backend/backend.php';

class News extends Backend {

	public function __construct() {
		parent::__construct();
		$this->load->model('news_model', 'model');
	}

	public function index() {
		$this->load->model('news_category_model');
		$data = array(
			'categories' => $this->news_category_model->records()
		);
		$this->load_js(array('jquery-ui-1.8.20.custom.min', 'backend_news'));
		$this->load_css(array('custom-theme/jquery-ui-1.8.20.custom', 'backend_news_index'));

		$this->rend('news', $data);
	}

	public function at_home() {
		$this->load->helper('text');
		$data = array(
			'news' => array(),
			'l_position' => array(
				array('name' => '首页管理'),
				array('name' => '首页新闻设置')
			)
		);
		$this->load->model('kvs_model');
		$ids = $this->kvs_model->getNewsAtHome();
		$conditions = array(
			array('where_in', 'id', array_values($ids))
		);
		$news = $this->model->records(array(), $conditions, array(), 6);
		foreach ($news as $item) {
			$key = array_search($item->id, $ids);
			$data['news'][$key] = $item;
		}
		unset($news);
		$this->load_js('backend_news_at_home');
		$this->load_css('backend_news_at_home');
		$this->rend('news_at_home', $data);
	}

	public function setNewsAtHome() {
		$index = (int) $this->input->get('index');
		$id = (int) $this->input->get('id');

		if (empty($id)) {
			$this->json_back('XT002', '参数错误');
		}

		$this->load->model('kvs_model');

		if ($this->kvs_model->setNewsAtHome($index, $id)) {
			$this->json_back('XT001', '设置成功');
		}

		$this->json_back('XT002', '数据库错误');
	}

	public function records() {
		$page = intval($this->input->get('page'));
		$page_size = intval($this->input->get('pageSize'));
		$page = $page ? $page : 1;
		$page_size = $page_size ? $page_size : 5;
		$offset = $page_size * ($page - 1);

		$conditions = array();
		$id = intval($this->input->get('id'));
		if (!empty($id)) {
			$conditions[] = array('where', 'id', $id);
		}

		$category = intval($this->input->get('category'));
		if (!empty($category)) {
			$conditions[] = array('where', 'category', $category);
		}

		$title = trim($this->input->get('title'));
		if (!empty($title)) {
			$conditions[] = array('or_like', 'title_zh', $title);
			$conditions[] = array('or_like', 'title_fr', $title);
			$conditions[] = array('or_like', 'title_en', $title);
		}


		$labels = trim($this->input->get('labels'));
		if (!empty($labels)) {
			$conditions[] = array('or_like', 'labels_zh', $labels);
			$conditions[] = array('or_like', 'labels_fr', $labels);
			$conditions[] = array('or_like', 'labels_en', $labels);
		}

		$start = $this->input->get('start');
		if (!empty($start)) {
			$conditions[] = array('where', 'created_at >=', $start);
		}

		$end = $this->input->get('end');
		if (!empty($end)) {
			$conditions[] = array('where', 'created_at <=', $end);
		}

		$orders = array(array('created_at','desc'));
		$this->json_back('XT001', '', array(
			'total' => $this->model->total($conditions),
			'records' => $this->model->records(array(), $conditions, $orders, $page_size, $offset)
		));
	}

	public function add() {
		$this->load->model('news_category_model');

		$category_columns = array(
			array('id', 'id'),
			array('name_zh', 'name')
		);

		$data = array(
			'categories' => $this->news_category_model->records($category_columns),
			'l_position' => array(
				array('name' => '新闻列表', 'path' => 'backend/news'),
				array('name' => '新增新闻')
			)
		);

		$this->load_js(array(
			'jquery-ui-1.8.20.custom.min',
			'ImageBrowser',
			'ueditor/editor_config',
			'ueditor/editor_all_min',
			'backend_news_form'
		));

		$this->load_css(array(
			'custom-theme/jquery-ui-1.8.20.custom',
			'../js/ueditor/themes/default/ueditor',
			'backend_news_form'
		));
		$this->rend('news_form', $data);
	}

	public function insert() {
		$data = $this->getPostData();
		if (($id = $this->model->insert($data)) > 0) {
			$this->json_back('XT001', '添加成功', array('id' => $id));
		}
		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	public function edit() {
		$id = intval($this->uri->segment(4));

		$this->load->model('news_category_model');
		$category_columns = array(
			array('id', 'id'),
			array('name_zh', 'name')
		);

		$data = array(
			'news' => $this->model->find($id),
			'categories' => $this->news_category_model->records($category_columns)
		);

		$this->load_js(array(
			'jquery-ui-1.8.20.custom.min',
			'ImageBrowser',
			'ueditor/editor_config',
			'ueditor/editor_all_min',
			'backend_news_form'
		));

		$this->load_css(array(
			'custom-theme/jquery-ui-1.8.20.custom',
			'../js/ueditor/themes/default/ueditor',
			'backend_news_form'
		));

		$this->rend('news_form', $data);
	}

	public function update() {
		$id = intval($this->input->post('id'));
		if ($this->model->update($id, $this->getPostData())) {
			$this->json_back('XT001', '更新成功', array('id' => $id));
		}
		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	public function delete() {
		$id = intval($this->input->get('id'));
		if ($this->model->delete($id)) {
			$this->json_back('XT001', '删除成功。');
		}
		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	private function getPostData() {
		$data = array(
			'category' => intval($this->input->post('category')),
			'title_zh' => trim($this->input->post('title_zh')),
			'title_fr' => trim($this->input->post('title_fr')),
			'title_en' => trim($this->input->post('title_en')),
			'home_cover' => intval($this->input->post('home_cover')),
			'list_cover' => intval($this->input->post('list_cover')),
			'content_zh' => $this->input->post('content_zh'),
			'content_fr' => $this->input->post('content_fr'),
			'content_en' => $this->input->post('content_en'),
			'labels_zh' => trim($this->input->post('labels_zh')),
			'labels_fr' => trim($this->input->post('labels_fr')),
			'labels_en' => trim($this->input->post('labels_en')),
		);
		$created_at = trim($this->input->post('created_at'));
		if(!empty($created_at)){
			$data['created_at'] = $created_at;
		}
		return $data;
	}
	
	public function convert(){
		$id = (int) $this->input->get('id');
		$news = $this->model->find($id);
		$data = array(
			'title_fr' => $news->title_zh,
			'content_fr' => $news->content_zh,
			'labels_fr' => $news->labels_zh,
			'title_zh' => $news->title_fr,
			'content_zh' => $news->content_fr,
			'labels_zh' => $news->labels_fr
		);

		$result = $this->model->update($id, $data);
		var_dump($result);
	}

}
